package com.qq.spirit;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.ProtocolException;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.RedirectStrategy;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.protocol.HttpContext;

import com.qq.spirit.db.DBUtil;
import com.qq.spirit.model.UserData;

public class HttpClientUnit
{
	public static ConcurrentHashMap<Integer, DefaultHttpClient> userInfoCache = new ConcurrentHashMap<Integer, DefaultHttpClient>();

	static Properties p = new java.util.Properties();
	static
	{
		try
		{
			p.load(new FileInputStream(new File(DBUtil.class.getClassLoader().getResource("./config.property").getPath())));
		} catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static DefaultHttpClient getDefaultHttpClient(final int gid)
	{
		if (!userInfoCache.containsKey(gid))
		{
			synchronized (userInfoCache)
			{
				if (!userInfoCache.containsKey(gid))
				{
					DefaultHttpClient httpClient = new DefaultHttpClient();
					if (p.getProperty("use_proxy").equals("1"))
					{
						HttpHost host = new HttpHost(p.getProperty("proxy_host"), Integer.parseInt(p.getProperty("proxy_port")));
						httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, host);
						CredentialsProvider credsProvider = new BasicCredentialsProvider();
						// 设定验证内容
						UsernamePasswordCredentials creds = new UsernamePasswordCredentials(p.getProperty("proxy_username"), p.getProperty("proxy_password"));
						// 创建验证
						credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), creds);
						httpClient.setCredentialsProvider(credsProvider);
					}

					httpClient.setCookieStore(new CookieStore()
					{

						@Override
						public List<Cookie> getCookies()
						{
							// TODO Auto-generated method stub
							return new ArrayList<Cookie>();
						}

						@Override
						public boolean clearExpired(Date arg0)
						{
							// TODO Auto-generated method stub
							return false;
						}

						@Override
						public void clear()
						{
							// TODO Auto-generated method stub

						}

						@Override
						public void addCookie(Cookie arg0)
						{
							// TODO Auto-generated method stub

						}
					});

					userInfoCache.put(gid, httpClient);
				}
			}
		}
		return userInfoCache.get(gid);
	}

}
